/**
 * Created by oneminute on 7/30/14.
 */

var lz = require("../lib/utils/modules")();
var path = require("path");
var fs = require("fs");

exports.get = function (_req, _res, _opts, _callback) {
    var device = _req.query.device;
    if (!lz.Config.devices.hasOwnProperty(device)) {
        lz.Logger.error("/version: 未找到用户请求的设备。", device);
        _callback(lz.Config.errors.getError(lz.Config.errors.REQUEST_NO_DEVICE));
        return;
    }
    /*var filepath = path.normalize(path.join(__dirname, '../public/apps', device, 'version.json'));
     lz.Logger.debug('/version: 版本文件路径: ', filepath);
     fs.readFile(filepath, { encoding: 'utf-8'}, function (_err, _data) {
     lz.Logger.debug('/version:readfile: ', _data);
     if (_err) {
     _callback(lz.Config.errors.getError(lz.Config.errors.SYSTEM_ERROR));
     } else {
     var info = JSON.parse(_data);
     info.url = '/apps/' + device + '/' + info.version + "/IWANT.apk";
     _callback(null, _opts.user, info);
     }
     });*/
    lz.Utils.getAppVersion(device, function (_err, _info) {
        if (_err) {
            lz.Logger.error("/version: 获取版本号时发生错误.");
            _callback(_err);
            return;
        }

        _callback(null, _opts.user, _info);
    });
};
